//
//  TJToolsbarView.swift
//  WeiBo
//
//  Created by TanJian on 15/12/10.
//  Copyright © 2015年 Joshpell. All rights reserved.
//

import UIKit

//工具视图的创建
class TJToolsbarView: UIView {
    
    //三个按钮控件
    var retweetBtn:UIButton?
    var commentBtn:UIButton?
    var unlikeBtn:UIButton?
    
    //视图模型,获取到模型时赋值
    var statusViewModel:TJUserDataViewModel?{
        didSet{
            
            retweetBtn?.setTitle(statusViewModel?.retweetStr, forState: .Normal)
            commentBtn?.setTitle(statusViewModel?.commentStr, forState: .Normal)
            unlikeBtn?.setTitle(statusViewModel?.attitudeStr, forState: .Normal)
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
    
        //添加按钮和分割线
        retweetBtn = addButton("转发",imgName: "timeline_icon_retweet")
        commentBtn = addButton("评论",imgName: "timeline_icon_comment")
        unlikeBtn = addButton("赞",imgName: "timeline_icon_unlike")
        
        let spl1 = addLine()
        let spl2 = addLine()
        
        //添加约束
        retweetBtn?.snp_makeConstraints(closure: { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentBtn!)
        })
        
        commentBtn?.snp_makeConstraints(closure: { (make) -> Void in
            make.leading.equalTo(retweetBtn!.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeBtn!)
        })
        
        unlikeBtn?.snp_makeConstraints(closure: { (make) -> Void in
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentBtn!.snp_trailing)
        })
        
        spl1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetBtn!.snp_trailing)
            make.centerY.equalTo(self)
        }
        spl2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentBtn!.snp_trailing)
            make.centerY.equalTo(self)
        }
        
    }
    
    //三个btn一样，抽取添加方法
    private func addButton(title:String,imgName:String) -> UIButton{
        
        let button = UIButton()
        
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Highlighted)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setImage(UIImage(named: imgName), forState: .Normal)

        //背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        
        
        
        addSubview(button)
        return button
    }
    
    private func addLine() -> UIView {
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        
        return imageView
    }
    
    
}










